package com.zhl.scheduling.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhl.scheduling.mapper.WorkpieceCategoryMapper;
import com.zhl.scheduling.mapper.WorkpieceMapper;
import com.zhl.scheduling.model.bo.WorkpieceListQueryBO;
import com.zhl.scheduling.model.entity.Workpiece;
import com.zhl.scheduling.model.entity.WorkpieceCategory;
import com.zhl.scheduling.model.vo.ListVO;
import com.zhl.scheduling.model.vo.ResStatus;
import com.zhl.scheduling.model.vo.ResultVO;
import com.zhl.scheduling.service.WorkpieceService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: ZhangLu In DLUT
 * @since: 1.0.0
 * @Description: 工件操作接口的实现类
 */
@Service
public class WorkpieceServiceImpl implements WorkpieceService {

    @Resource
    private WorkpieceMapper workpieceMapper;

    @Resource
    private WorkpieceCategoryMapper workpieceCategoryMapper;

    /**
     * 根据客户端封装的工件查询条件，查询相应的机器
     * @param workpieceListQueryBO 客户端封装的查询工件实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO queryByConditions(WorkpieceListQueryBO workpieceListQueryBO) {

        Example example = new Example(Workpiece.class);
        Example.Criteria criteria = example.createCriteria();
        if (workpieceListQueryBO.getWorkpieceId() != null && !workpieceListQueryBO.getWorkpieceId().equals("")) {
            criteria.andEqualTo("workpieceId", workpieceListQueryBO.getWorkpieceId());
        }
        if (workpieceListQueryBO.getWorkpieceCategoryId() != null && !workpieceListQueryBO.getWorkpieceCategoryId().equals("")) {
            criteria.andEqualTo("workpieceCategoryId", workpieceListQueryBO.getWorkpieceCategoryId());
        }
        if (workpieceListQueryBO.getWorkpieceName() != null && !workpieceListQueryBO.getWorkpieceName().equals("")) {
            criteria.andLike("workpieceName", "%"+workpieceListQueryBO.getWorkpieceName()+"%");
        }
        if (workpieceListQueryBO.getWorkpieceStatus() != null && !workpieceListQueryBO.getWorkpieceStatus().equals("")) {
            criteria.andEqualTo("workpieceStatus", workpieceListQueryBO.getWorkpieceStatus());
        }
        if (workpieceListQueryBO.getProcessFlexibility() != null && !workpieceListQueryBO.getProcessFlexibility().equals("")) {
            criteria.andEqualTo("processFlexibility", workpieceListQueryBO.getProcessFlexibility());
        }
        if (workpieceListQueryBO.getOrderBy() != null && !workpieceListQueryBO.getOrderBy().equals("") ) {
            Example.OrderBy orderBy = example.orderBy(workpieceListQueryBO.getOrderBy().split(" ")[0]);
            if (workpieceListQueryBO.getOrderBy().split(" ")[1].equals("asc")){
                orderBy.asc();
            } else {
                orderBy.desc();
            }
        }
        Page<Workpiece> list = PageHelper.startPage(workpieceListQueryBO.getPage(), workpieceListQueryBO.getLimit()).doSelectPage(() -> workpieceMapper.selectByExample(example));
        if (list != null) {
            ListVO listVO = new ListVO();
            listVO.setItems(list);
            listVO.setTotal(PageHelper.count(() -> workpieceMapper.selectByExample(example)));
            return new ResultVO(ResStatus.OK,"获取列表成功", listVO);
        }
        return new ResultVO(ResStatus.NO,"获取列表失败", null);
    }

    /**
     * 根据客户端提供的工件信息更新相应的工件
     * @param workpiece 客户端封装的工件实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO update(Workpiece workpiece) {
        workpiece.setUpdatedTime(new Date());
        int updateNum = workpieceMapper.updateByPrimaryKey(workpiece);
        if (updateNum > 0) {
            return new ResultVO(ResStatus.OK,"更新数据成功", null);
        }else {
            return new ResultVO(ResStatus.NO,"更新数据失败", null);
        }

    }

    /**
     * 根据客户端提供的工件信息添加工件
     * @param workpiece 客户端封装的工件实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO add(Workpiece workpiece) {
        workpiece.setUpdatedTime(new Date());
        workpiece.setCreateTime(new Date());
        int insertNum = workpieceMapper.insert(workpiece);
        if (insertNum > 0) {
            return new ResultVO(ResStatus.OK, "增加数据成功", null);
        }else {
            return new ResultVO(ResStatus.OK,"更新数据失败", null);
        }
    }

    /**
     * 根据客户端提供的工件类别信息增加工件
     * @param workpieceCategory 客户端封装的工件类别实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO addCategory(WorkpieceCategory workpieceCategory) {
        int insertNum = workpieceCategoryMapper.insert(workpieceCategory);
        if (insertNum > 0) {
            return new ResultVO(ResStatus.OK, "增加数据成功", null);
        }else {
            return new ResultVO(ResStatus.OK,"更新数据失败", null);
        }
    }
}
